﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Ev.Answer
{
    public partial class TypeForm : Form
    {
        //初始化xml
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlRoot;

        public TypeForm()
        {
            InitializeComponent();
        }

        private void AnswerForm_Load(object sender, EventArgs e)
        {
            InitDataBtn();
        }

        void InitDataBtn()
        {
            if (File.Exists(StaticData.xmlUrlPath))
                xmlDoc.Load(StaticData.xmlUrlPath);
            else
                xmlDoc.LoadXml("<XmlConfig />");
            xmlRoot = (XmlElement)xmlDoc.ChildNodes[1];

            var xmlItemsType = xmlRoot.SelectNodes(StaticData.TypeNode);
            if (xmlItemsType == null)
            {
                MessageBox.Show("当前无分类信息现在去添加？");

                Setting s = new Setting();
                s.ShowDialog();
                InitDataBtn();
                return;
            }


            var btnWidth = 150;
            var a = 0;
            for (int i = 0; i < xmlItemsType.Count; i++)
            {
                var nodeXml = xmlItemsType[i];

                Button btn = new Button();
                btn.Text = nodeXml.Attributes["Name"].Value;
                btn.Tag = nodeXml.Attributes["sIndex"].Value;
                btn.Size = new Size(btnWidth, 50);
                btn.Click += Btn_Click;

                //每行个数
                var s = groupBox1.Width / (btnWidth + 20);
                var b = i / s;
                a = i < s ? i : (i % s);
                btn.Location = new Point((btnWidth + 20) * a + 20, b * 70 + 30);
                groupBox1.Controls.Add(btn);
            }
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            var btnId = Convert.ToInt32(btn.Tag);

            AnswerForm af = new AnswerForm();
            af.sIndexType = btnId;
            af.ShowDialog();
        }
    }
}
